using System;
using System.Collections.Generic;
using System.Reflection;

namespace Charisma.Conventions
{
    public class PublicMemberConvention : IMemberConvention
    {
        #region IMemberConvention Members

        public IEnumerable<PropertyInfo> Find(Type type)
        {
            PropertyInfo[] candidates =
                type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

            foreach (PropertyInfo candidate in candidates)
            {
                if (candidate.GetIndexParameters().Length != 0)
                    continue;

                MethodInfo getMethod = candidate.GetGetMethod();
                if (getMethod == null || !getMethod.IsPublic)
                    continue;

                if (getMethod.IsVirtual && getMethod.GetBaseDefinition().DeclaringType != type)
                    continue;

                MethodInfo setMethod = candidate.GetSetMethod();
                if (setMethod == null)
                    continue;

                yield return candidate;
            }
        }

        #endregion
    }
}